<?php 
class cls_serve_address extends cls_serve_base
{
	function check_if_related_data($customerid, $addressid)
	{
		$address_data = cls_data_base::get_instance($this->name);
		
		$result = $address_data->check_if_related_data($customerid, $addressid);
		
		return $result;
	}
	
	function get_all_address($customerid)
	{
		$address_data = cls_data_base::get_instance($this->name);
	
		$result = $address_data->get_all_address($customerid);
		if(!$result)
		{
			return return_info(true);
		}
		else
		{
			return return_info(true, $result);
		}
	}
	
	function get_default_addressid($customerid)
	{
		$address_data = cls_data_base::get_instance($this->name);
		
		$result = $address_data->get_default_addressid($customerid);
		
		if(!$result)
		{
			return return_info(false, "获取默认地址ID失败");
		}
		else
		{
			return return_info(true, $result);
		}
	}
	
	function get_default_address($customerid)
	{
		$address_data = cls_data_base::get_instance($this->name);
	
		$result = $address_data->get_default_address($customerid);
	
		if(!$result)
		{
			return return_info(false, "获取默认地址失败");
		}
		else
		{
			return return_info(true, $result);
		}
	}
	
	function add_address($customerid, $postal, $receiver, $phone, $address, $description)
	{
		$address_data = cls_data_base::get_instance($this->name);
	
		$result = $address_data->add_address($customerid, $postal, $receiver, $phone, $address, $description);

		if ($result >0)
		{
			$default_address = $this->get_default_addressid($customerid);
			if($default_address['success'] == true && $default_address['ret']['DefaultAddressID'] == 0)
			{
				$this->set_default_address($customerid, $result);
			}
			return return_info(true, $result);
		}
		else
		{
			return return_info(false);
		}
	}
	
	function delete_address($customerid, $addressid)
	{
		$related = $this->check_if_related_data($customerid, $addressid);
		if(!related)
		{
			return return_info(false, "该地址不属于当前用户");
		}
		
		$address_data = cls_data_base::get_instance($this->name);
	 
		$result = $address_data->delete_address($addressid);
	
		if($result == 1)
		{
			echo 1;
			$default_address = $this->get_default_addressid($customerid);
			echo json_encode($default_address);
			if($default_address['ret']['DefaultAddressID'] == $addressid)
			{
				$all_address = $this->get_all_address($customerid);
				if($all_address['ret'] == null)
				{
					$this->set_default_address($customerid, 0);
				}
				else
				{
					$this->set_default_address($customerid, $all_address['ret'][0]['ID']);
				}
			}
			return return_info(true);
		}
		else
		{
			return return_info(false);
		}
	}
	
	function update_address($customerid, $addressid, $postal, $receiver, $phone, $address, $description)
	{
		$related = $this->check_if_related_data($customerid, $addressid);
		if(!related)
		{
			return return_info(false, "该地址不属于当前用户");
		}
		
		$address_data = cls_data_base::get_instance($this->name);
	
		$result = $address_data->update_address($addressid, $postal, $receiver, $phone, $address, $description);
	
		if($result == 1)
		{
			return return_info(true);
		}
		else
		{
			return return_info(false);
		}
	}
	
	function set_default_address($customerid, $addressid)
	{
		if($addressid != 0)
		{
			$related = $this->check_if_related_data($customerid, $addressid);
			if(!$related)
			{
				return return_info(false, "该地址不属于当前用户");
			}
		}
		
		$address_data = cls_data_base::get_instance($this->name);
	
		$result = $address_data->set_default_address($customerid, $addressid);
	
		if($result == 1)
		{
			return return_info(true);
		}
		else
		{
			return return_info(false);
		}
	}
}
?>